<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>tomcat单机多实例 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>tomcat单机多实例</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需求是： 同一个tomcat服务下，可以访问多个域名。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先来看看tomcat配置文件server.xml的结构</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;Server&gt;</div><div class="line"></div><div class="line">    &lt;Service name=”xxx”&gt;</div><div class="line"></div><div class="line">        &lt;Host  name=”aaa.com” appBase=”dir1″&gt;</div><div class="line"></div><div class="line">        &lt;/Host&gt;</div><div class="line"></div><div class="line">    &lt;/Service&gt;</div><div class="line"></div><div class="line">&lt;Server&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Service下面可以定义监听的端口，包含了<host></host>，而<host></host>里面定义虚拟主机，即，定义站点的域名和站点的目录。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两种方案：</p>
<ol>
<li><p>可以在<service></service>里面定义多个<host></host>，这样就定义了多个站点，但端口不能变。</p>
</li>
<li><p>可以在<server></server>里面定义多个<service></service>，<service></service>里再定义<host></host>，这样做的好处是，可以更改监听端口。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假设需求为：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同一个服务器上跑两个站点</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;www.aaa.com  监听端口是8080</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;www.bbb.com  监听端口是8081</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置文件内容为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div></pre></td><td class="code"><pre><div class="line">&lt;GlobalNamingResources&gt;</div><div class="line">&lt;!– Editable user database that can also be used by</div><div class="line">UserDatabaseRealm to authenticate users</div><div class="line">–&gt;</div><div class="line">&lt;Resource name=”UserDatabase” auth=”Container”</div><div class="line"><span class="built_in">type</span>=”org.apache.catalina.UserDatabase”</div><div class="line">description=”User database that can be updated and saved”</div><div class="line">factory=”org.apache.catalina.users.MemoryUserDatabaseFactory”</div><div class="line">pathname=”conf/tomcat-users.xml” /&gt;</div><div class="line">&lt;/GlobalNamingResources&gt;</div><div class="line"></div><div class="line">&lt;!– A “Service” is a collection of one or more “Connectors” that share</div><div class="line">a single “Container” Note: A “Service” is not itself a “Container”,</div><div class="line">so you may not define subcomponents such as “Valves” at this level.</div><div class="line">Documentation at /docs/config/service.html</div><div class="line">–&gt;</div><div class="line">&lt;Service name=”Catalina”&gt;</div><div class="line"></div><div class="line">&lt;Connector port=”8080″ protocol=”HTTP/1.1″</div><div class="line">connectionTimeout=”20000″</div><div class="line">redirectPort=”8443″ /&gt;</div><div class="line">&lt;Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ /&gt;</div><div class="line"></div><div class="line">&lt;Engine name=”Catalina” defaultHost=”localhost”&gt;</div><div class="line"></div><div class="line">&lt;Realm className=”org.apache.catalina.realm.LockOutRealm”&gt;</div><div class="line">&lt;Realm className=”org.apache.catalina.realm.UserDatabaseRealm”</div><div class="line">resourceName=”UserDatabase”/&gt;</div><div class="line">&lt;/Realm&gt;</div><div class="line"></div><div class="line">&lt;Host name=”www.aaa.com” appBase=”webapps”</div><div class="line">unpackWARs=”<span class="literal">true</span>” autoDeploy=”<span class="literal">true</span>”&gt;</div><div class="line"></div><div class="line">&lt;Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”</div><div class="line">prefix=”aaa.com_access” suffix=”.<span class="built_in">log</span>”</div><div class="line">pattern=”%h %l %u %t &amp;quot;%r&amp;quot; %s %b” /&gt;</div><div class="line"></div><div class="line">&lt;/Host&gt;</div><div class="line">&lt;/Engine&gt;</div><div class="line">&lt;/Service&gt;</div><div class="line">&lt;Service name=”aming”&gt;</div><div class="line"></div><div class="line">&lt;Connector port=”8081″ protocol=”HTTP/1.1″</div><div class="line">connectionTimeout=”20000″</div><div class="line">redirectPort=”8443″ /&gt;</div><div class="line"></div><div class="line">&lt;Engine name=”aming” defaultHost=”localhost”&gt;</div><div class="line"></div><div class="line">&lt;Realm className=”org.apache.catalina.realm.LockOutRealm”&gt;</div><div class="line">&lt;Realm className=”org.apache.catalina.realm.UserDatabaseRealm”</div><div class="line">resourceName=”UserDatabase”/&gt;</div><div class="line">&lt;/Realm&gt;</div><div class="line"></div><div class="line">&lt;Host name=”www.bbb.com” appBase=”/data/tomcat”</div><div class="line">unpackWARs=”<span class="literal">true</span>” autoDeploy=”<span class="literal">true</span>”&gt;</div><div class="line"></div><div class="line">&lt;Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”</div><div class="line">prefix=”bbb.com_access” suffix=”.<span class="built_in">log</span>”</div><div class="line">pattern=”%h %l %u %t &amp;quot;%r&amp;quot; %s %b” /&gt;</div><div class="line"></div><div class="line">&lt;/Host&gt;</div><div class="line">&lt;/Engine&gt;</div><div class="line">&lt;/Service&gt;</div><div class="line"></div><div class="line">&lt;/Server&gt;</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Tomcat/12.tomcat单机多实例/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
